#include "forgetpassword.h"
#include "ui_forgetpassword.h"
#include "../MainProject/sqlitedatabase.h"
#include "QMessageBox"
#include <QCryptographicHash>
#include <QRandomGenerator>
#include <QDebug>
ForgetPassword::ForgetPassword(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ForgetPassword)
{
    ui->setupUi(this);
       window()->setWindowTitle("修改密码");
}

ForgetPassword::~ForgetPassword()
{
    delete ui;
}

void ForgetPassword::on_pushButton_clicked()
{   QString password;
    //显示
    QString phone=ui->UserNameEdit->text();
    QString passwords=ui->passwordEdit->text();
    QString yam=ui->yzmEdit_2->text();
    password=hashPassword(passwords);
    if(phone.isEmpty()){
         QMessageBox::information(this, "waring", "请输入手机号");
    }
    if(!phone.isEmpty()){
        //查询手机号码
        SQLiteDatabase* db = SQLiteDatabase::getInstance();
        QString condition = QString("phone = '%1'").arg(phone); // 设置条件字符串
        QJsonArray result = db->read("users", condition); // 调用read方法获取结果
        if(result.isEmpty()){
            QMessageBox::information(this, "waring", "没有该手机号");
            return;
        }
        if(yam.isEmpty()){
              QMessageBox::information(this, "waring", "请输入验证码");
        }

        if(yam==randomString && !yam.isEmpty()){
            if(passwords.isEmpty()){
                  QMessageBox::information(this, "waring", "修改的密码不能为空");
            }
            if(!passwords.isEmpty() && !phone.isEmpty()){
                //执行修改操作
                QString condition = QString("phone = '%1'").arg(phone); // 设置条件字符串
                QJsonObject data;
                data["password"] = password;

                //执行修改操作
                db->update("users", data, condition);
                QMessageBox::information(this, "waring", "密码修改成功");
                this->hide();
            }
        }
        else if(yam!=randomString &&!yam.isEmpty()){
             QMessageBox::information(this, "waring", "验证码错误");
        }


        }
}

QString ForgetPassword::hashPassword(const QString &password)
{
    // 创建一个 QCryptographicHash 对象，设置其使用 MD5 算法
    QCryptographicHash hasher(QCryptographicHash::Md5);

    // 添加密码数据到哈希对象中（注意：转为 QByteArray）
    hasher.addData(password.toUtf8());

    // 计算哈希值并获取结果，转换为十六进制格式字符串
    QString hashedPassword = hasher.result().toHex();

    return hashedPassword;
}


void ForgetPassword::on_yanzhengmaButton_clicked()
{    QString phone=ui->UserNameEdit->text();
    if(phone.isEmpty()){
         QMessageBox::information(this, "waring", "请输入手机号码");
        return;
    }else{
        //查询手机号码
        SQLiteDatabase* db = SQLiteDatabase::getInstance();
        QString condition = QString("phone = '%1'").arg(phone); // 设置条件字符串
        QJsonArray result = db->read("users", condition); // 调用read方法获取结果
        if(result.isEmpty()){
             QMessageBox::information(this, "waring", "没有该手机号码");
            return;
        }
    }
    // 通过QRandomGenerator生成五位随机数
    QRandomGenerator generator;
    int randomNumber = generator.bounded(10000, 99999); // 生成10000到99999之间的随机数

     randomString = QString::number(randomNumber); // 将整数转换为QString类型的字符串

    qDebug()<<randomString;

}

